/**
 * 文章相关请求模块
 */

import request from '@/utils/request'
import {
  Article,
  ArticleCategory,
  ArticleForm,
  ArticleQuery,
  CategoryForm,
  CategoryQuery
} from './types/article'
import { IFormRule } from './types/form'

/**
 * 给文章设置关联产品
 * @param id
 * @param product_id
 * @returns
 */
export const relationProductArticle = (id: number, product_id: number) => {
  return request({
    method: 'PUT',
    url: `/cms/cms/relation/${id}`,
    data: {
      product_id
    }
  })
}

/**
 * 取消文章的商品关联
 * @param id
 * @param product_id
 * @returns
 */
export const unrelationProductArticle = (id: number) => {
  return request({
    method: 'PUT',
    url: `/cms/cms/unrelation/${id}`
  })
}

/**
 * 删除文章
 * @param id
 * @returns
 */
export const deleteArticle = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/cms/cms/${id}`
  })
}
/**
 * 获取文章列表
 * @param params
 * @returns
 */
export const getArticles = (params: ArticleQuery) => {
  return request<{
    count: number
    list: Article[]
  }>({
    method: 'GET',
    url: '/cms/cms',
    params
  })
}

/**
 * 获取单篇文章信息
 * @param id
 * @returns
 */
export const getArticleById = (id: number) => {
  return request<{ info: Article }>({
    method: 'GET',
    url: `/cms/cms/${id}`
  })
}

/**
 * 新增或修改文章(保存文章)
 * @param data
 * @returns
 */
export const saveArticle = (data: ArticleForm) => {
  return request({
    method: 'POST',
    url: '/cms/cms',
    data
  })
}

/**
 * 修改文章分类状态
 * @param id
 * @param status
 * @returns
 */
export const updateArticleCateggoryStatus = (id: number, status: number) => {
  return request<Article>({
    method: 'PUT',
    url: `/cms/category/set_status/${id}/${status}`
  })
}

/**
 * 获取分类列表
 * @param params
 * @returns
 */
export const getArticleCategorys = (params: CategoryQuery) => {
  return request<{
    count: number
    list: ArticleCategory[]
  }>({
    method: 'GET',
    url: '/cms/category',
    params
  })
}

/**
 * 删除文章分类
 * @param id
 * @returns
 */
export const deleteArticleCategory = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/cms/category/${id}`
  })
}

/**
 * 新增文章分类
 * @param data
 * @returns
 */
export const saveArticleCategory = (data: CategoryForm) => {
  return request({
    method: 'POST',
    url: '/cms/category',
    data
  })
}

/**
 * 编辑修改文章分类
 * @param data
 * @returns
 */
export const updateArticleCategory = (id: number, data: CategoryForm) => {
  return request({
    method: 'PUT',
    url: `/cms/category/${id}`,
    data
  })
}

/**
 * 获取文章分类创建表单检验规则信息
 * @returns
 */
export const getArticleCategoryCreateRule = () => {
  return request<{ rules: IFormRule[] }>({
    method: 'GET',
    url: '/cms/category/create'
  }).then(data => {
    return data.rules
  })
}

/**
 * 获取文章分类编辑表单检验规则信息
 * @returns
 */
export const getArticleCategoryEditRule = (id: number) => {
  return request<{ rules: IFormRule[] }>({
    method: 'GET',
    url: `/cms/category/${id}/edit`
  }).then(data => {
    return data.rules
  })
}
